package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.HttpStatusEnum;
import com.example.entity.VoChatMsg;
import com.example.entity.ResponseData;
import com.example.pojo.Chat;
import com.example.mapper.ChatMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 聊天信息表(Chat)表服务实现类
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@Service
public class ChatService {
    /**
     * mapper对象
     */
    @Resource
    private ChatMapper chatMapper;

    /**
     * 分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param chat        筛选条件
     * @return
     */
    public ResponseData getByPage(Integer currentPage, Integer pageSize, Chat chat) {
        //TODO: getByPage chat qw查询条件待构建
        LambdaQueryWrapper<Chat> lqw = new LambdaQueryWrapper<>();
        lqw.eq(chat.getDeleted() !=null, Chat::getDeleted, chat.getDeleted());
        lqw.and(qr->qr.and(wrapper->wrapper.eq(Chat::getFromId,chat.getFromId()).eq(Chat::getToId,chat.getToId()))
                .or(wrapper->wrapper.eq(Chat::getFromId,chat.getToId()).eq(Chat::getToId,chat.getFromId())));
        lqw.orderByDesc(Chat::getTime);
        //查找
        IPage ipage = new Page(currentPage, pageSize);
        chatMapper.selectPage(ipage, lqw);
        //若当前页码大于总页面数
        if (currentPage > ipage.getPages()) {
            ipage = new Page(ipage.getPages(), pageSize);
            chatMapper.selectPage(ipage, lqw);
        }
        if (ipage.getTotal() <= 0) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        Collections.reverse(ipage.getRecords());
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), ipage);
    }

    /**
     * 根据ID查询信息
     *
     * @param id 主键
     * @return 单条数据
     */
    public ResponseData findById(Long id) {
        Chat chat = chatMapper.selectById(id);
        if (chat == null || chat.getId() == null) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), chat);
    }

    /**
     * 新增数据
     *
     * @param chat 实体
     * @return 新增结果
     */
    public ResponseData add(Chat chat) {
        //插入
        if (chatMapper.insert(chat) > 0) {
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "新增失败，请稍后再试");
    }

    /**
     * 更新数据
     *
     * @param chat 实体
     * @return 更新结果
     */
    public ResponseData update(Chat chat) {
        //更新
        if (chatMapper.updateById(chat) > 0) {
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "修改失败，请稍后再试");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return
     */
    public ResponseData delete(Long id) {
        if (chatMapper.deleteById(id) > 0) {
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseData deleteBatch(List<Long> idList) {
        if (chatMapper.deleteBatchIds(idList) > 0) {
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 设置已读
     *
     * @param fromId
     * @param toId
     * @return
     */
    public ResponseData setRead(Long fromId, Long toId) {
        // 设置匹配条件
        UpdateWrapper<Chat> uw = new UpdateWrapper<>();
        uw.eq("from_id", toId);
        uw.eq("to_id", fromId);
        uw.set("seen", 1);
        // 开始更新
        chatMapper.update(null, uw);
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
    }

    /**
     * 获取聊天信息页列表
     * @param userId
     * @return
     */
    public ResponseData getChatMsgList(Long userId) {
        List<VoChatMsg> voChatMsgs = chatMapper.selectChatMsgList(userId);
        if (voChatMsgs.size()<=0){
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), voChatMsgs);
    }
}
